#!/bin/bash
# Print LAN/WAN IPv4 and IPv6 lines for Armbian MOTD.
# Standalone: discovers addresses itself and prints formatted output.

set -o pipefail

safe_source() { [[ -f "$1" ]] && . "$1"; }

THIS_SCRIPT="ip-info"
MOTD_DISABLE=""

# Defaults (can be overridden by /etc/default/armbian-motd if present)
HIDE_IP_PATTERN='^dummy0|^lo|^docker|^hassio|^br-|^veth|^vnet|^virbr'
HIDE_LOCAL_IPV6="true"

# Optional overrides
safe_source /etc/default/armbian-motd
for f in ${MOTD_DISABLE}; do
	[[ "${f}" == "${THIS_SCRIPT}" ]] && exit 0
done

# one-time spacer after header (marks that something printed before Performance)
_motd_mark_after_header() {
  local base="/run/armbian-motd"
  mkdir -p "$base" 2>/dev/null || base="${XDG_RUNTIME_DIR:-/tmp}/armbian-motd"
  mkdir -p "$base" 2>/dev/null || return 0
  local stamp="${base}/.after_header.printed"
  [[ -e "$stamp" ]] || { echo ""; : >"$stamp"; }
}

get_wan_address()  { curl -4 -fsS --max-time 2 https://ipv4.whatismyip.akamai.com/ 2>/dev/null || true; }
get_wan6_address() { curl -6 -fsS --max-time 2 https://ipv6.whatismyip.akamai.com/ 2>/dev/null || true; }

get_lan_ips() {
  # echo "<ipv4s space-separated>|<ipv6s space-separated>"
  local ipv4s=() ipv6s=() intf

  for p in /sys/class/net/*; do
    intf="${p##*/}"
    [[ "$intf" =~ $HIDE_IP_PATTERN ]] && continue

    # IPv4 (order preserved, unique)
    mapfile -t add4 < <(ip -4 -o addr show dev "$intf" 2>/dev/null \
      | awk '$3=="inet"{print $4}' | cut -d/ -f1 || true)
    for a in "${add4[@]}"; do
      [[ " ${ipv4s[*]} " == *" $a "* ]] || ipv4s+=("$a")
    done

    # IPv6 (prefer scope global when hiding link-local)
    if [[ "${HIDE_LOCAL_IPV6}" == "true" ]]; then
      mapfile -t add6 < <(ip -6 -o addr show dev "$intf" scope global 2>/dev/null \
        | awk '$3=="inet6"{print $4}' | cut -d/ -f1 || true)
    else
      mapfile -t add6 < <(ip -6 -o addr show dev "$intf" 2>/dev/null \
        | awk '$3=="inet6"{print $4}' | cut -d/ -f1 || true)
    fi
    for a in "${add6[@]}"; do
      [[ " ${ipv6s[*]} " == *" $a "* ]] || ipv6s+=("$a")
    done
  done

  printf '%s|%s\n' "${ipv4s[*]}" "${ipv6s[*]}"
}

print_ip_lines() {
  local ipv4s="$1" ipv6s="$2" wan4="$3" wan6="$4"
  # print a single blank line before the first post-header section
  _motd_spacer() {
    local tag="${1:-after-header}"
    local dir="/run/armbian-motd"; local stamp="$dir/.${tag}.printed"
    mkdir -p "$dir" 2>/dev/null
    [[ -e "$stamp" ]] || { echo ""; : > "$stamp"; }
  }

  # Drop WAN duplicates from LAN lists
  if [[ -n "$wan4" && " $ipv4s " == *" $wan4 "* ]]; then
    ipv4s="$(printf '%s\n' $ipv4s | awk -v w="$wan4" '$0!=w' | paste -sd' ' -)"
  fi
  if [[ -n "$wan6" && " $ipv6s " == *" $wan6 "* ]]; then
    ipv6s="$(printf '%s\n' $ipv6s | awk -v w="$wan6" '$0!=w' | paste -sd' ' -)"
  fi

  # IPv4 line
  if [[ -n "$ipv4s" || -n "$wan4" ]]; then
    _motd_mark_after_header
    local line=" IPv4:        "
    [[ -n "$ipv4s" ]] && line+=" \x1B[93m(LAN)\x1B[0m \x1B[92m${ipv4s// /, }\x1B[0m "
    [[ -n "$wan4"  ]] && line+="\x1B[93m(WAN)\x1B[0m \x1B[95m${wan4}\x1B[0m "
    printf '%b\n' "$line"
  fi

  # IPv6 line
  if [[ -n "$ipv6s" || -n "$wan6" ]]; then
    _motd_mark_after_header    
    local line=" IPv6:         "
    [[ -n "$ipv6s" ]] && line+="\x1B[96m${ipv6s// /, }\x1B[0m "
    [[ -n "$wan6"  ]] && line+="\x1B[93m(WAN)\x1B[0m \x1B[95m${wan6}\x1B[0m "
    printf '%b\n' "$line"
  fi
}

main() {
  local combo ipv4s ipv6s wan4 wan6
  combo="$(get_lan_ips)"
  IFS='|' read -r ipv4s ipv6s <<< "$combo"
  wan4="$(get_wan_address)"
  wan6="$(get_wan6_address)"
  print_ip_lines "$ipv4s" "$ipv6s" "$wan4" "$wan6"
}

# If sourced, don't run main; if executed, run.
(return 0 2>/dev/null) || main
